//
//  HomeRootViewController.m
//  gamebox_qpp
//
//  Created by 赵隆杰 on 2022/8/29.
//

#import "HomeRootViewController.h"
#import "HomeRootTableView.h"
#import "HomeRootTopView.h"
#import "HomeRootRankingListView.h"
#import "HomeRootViewModel.h"

@interface HomeRootViewController ()

@property(nonatomic,strong) HomeRootViewModel *viewModel;

@property (nonatomic, strong) HomeRootTableView *homeTableView;
@property (nonatomic, strong) HomeRootTopView *homeTopView;
@property (nonatomic, strong) HomeRootRankingListView *rankingListView;

@property (nonatomic, strong) UIView *topBgView;

@end

@implementation HomeRootViewController

- (HomeRootViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[HomeRootViewModel alloc] init];
    }
    return _viewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.naviView.hidden = YES;
    self.topBgView = [[UIView alloc] init];
    [self.view addSubview:self.topBgView];
    [self.topBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.view);
        make.height.mas_equalTo(400);
    }];
    
    self.homeTableView = [[HomeRootTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.view addSubview:self.homeTableView];
    [self.homeTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(-UIDevice.vg_tabBarFullHeight);
    }];
    
    self.rankingListView = [[HomeRootRankingListView alloc] init];
    [self.view addSubview:self.rankingListView];
    [self.rankingListView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.mas_equalTo([HomeRootTopView height]);
        make.bottom.mas_equalTo(-UIDevice.vg_tabBarFullHeight);
    }];
    
    self.homeTopView = [[HomeRootTopView alloc] init];
    [self.view addSubview:self.homeTopView];
    [self.homeTopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo([HomeRootTopView height]);
    }];
    self.homeTableView.hidden = NO;
    self.rankingListView.hidden = YES;
    
    VHWeakSelf
    self.homeTableView.homeroot_HeaderReloadBlock = ^{
        VHStrongSelf
        [self refreshData];
    };
    self.homeTopView.homeroot_btnClickBlock = ^(NSInteger tag) {
        VHStrongSelf
        if (tag == 100) {
            self.homeTableView.hidden = NO;
            self.rankingListView.hidden = YES;
            [self.homeTableView setContentOffset:CGPointZero];
        } else if (tag == 101) {
            self.homeTableView.hidden = YES;
            self.rankingListView.hidden = NO;
        }
    };
        
    [self refreshData];
}

- (void)refreshData
{
    //模拟请求
    VHWeakSelf
    [self.viewModel fetchHome:^{
        VHStrongSelf
        self.homeTableView.viewModel = self.viewModel;
        self.topBgView.backgroundColor = self.viewModel.topBgColor;
    }];
}

@end
